package com.rxt.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    //声明1个路由key 1个队列 1个交换机
    //延迟交换机
    public static final String DELAY_EXCHANGE_NAME = "delay.exchange";
    //延迟队列
    public static final String DELAY_QUEUE_NAME = "delay.queue";
    //延迟队列路由key
    public static final String DELAY_QUEUE_ROUTING_KEY = "delay.queue.routingkey";

    // 声明延迟交换机,由插件处理代码
    @Bean
    public TopicExchange lazyExchange() {
        Map<String, Object> pros = new HashMap<>();
        //设置交换机支持延迟消息推送
        pros.put("x-delayed-message", "direct");
        TopicExchange exchange = new TopicExchange("delay.exchange", true, false,pros);
        // 这一行是重点，指定交换机类型
        exchange.setDelayed(true);
        return exchange;
    }

    // 声明延迟队列
    @Bean
    public Queue delayQueueA() {
        return new Queue(DELAY_QUEUE_NAME);
    }

    // 声明延迟队列B的绑定关系
    @Bean
    public Binding delayBindingB() {
        return BindingBuilder.bind(delayQueueA())
                .to(lazyExchange())
                .with(DELAY_QUEUE_ROUTING_KEY);
    }

}
